#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

pair<int, vector<int>> LCIS(const vector<int> &A, const vector<int> &B)
{
	int n = A.size();
	int m = B.size();
	if (n == 0 || m == 0) {
		return { 0, {} };
	}

	// dp[j]：以 B[j] 结尾的 LCIS 长度
	vector<int> dp(m, 0);
	// prev[j]：在 LCIS 中 B[j] 的前驱元素在 B 中的索引
	vector<int> prev(m, -1);
	// 记录最大长度和结束索引
	int max_length = 0;
	int end_index = -1;

	for (int i = 0; i < n; i++) {
		int c = 0; // 当前满足 B[k] < A[i] 的最大 dp[k]
		int k_index = -1; // 与 c 对应的索引

		for (int j = 0; j < m; j++) {
			if (A[i] == B[j]) {
				// 发现公共元素，更新 dp 和 prev
				dp[j] = c + 1;
				prev[j] = k_index;

				// 更新全局最大值
				if (dp[j] > max_length) {
					max_length = dp[j];
					end_index = j;
				}
			} else if (A[i] > B[j]) {
				// 更新当前最大长度 c（满足 B[j] < A[i]）
				if (dp[j] > c) {
					c = dp[j];
					k_index = j;
				}
			}
		}
	}

	// 重建序列
	vector<int> sequence;
	int current = end_index;
	while (current != -1) {
		sequence.push_back(B[current]);
		current = prev[current];
	}
	reverse(sequence.begin(), sequence.end());

	return { max_length, sequence };
}

int max_len_dp(vector<int> &A, vector<int> &B, vector<int> &seq)
{
	int n = A.size();
	int m = B.size();
	vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
	vector<int> prev(m + 1, -1);

	int ans = 0;
	int max_pos = 0;
	for(int i = 1; i < n; i++){
		int so_len = 0;
		int pos = -1;
		for(int j = 1; j < m; j++){
			dp[i][j] = dp[i - 1][j];
			if(A[i] == B[j]){
				dp[i][j] = max(dp[i][j], so_len + 1);
				prev[j] = pos;
			}else if(B[j] < A[i]){
				if(dp[i - 1][j] > so_len){
					so_len = dp[i - 1][j];
					pos = j;
				}
			}
			if(dp[i][j] > ans){
				ans = dp[i][j];
				max_pos = j;
			}
		}
	}

	while(prev[max_pos] != -1){
		seq.push_back(max_pos);
		max_pos = prev[max_pos];
	}
	seq.push_back(max_pos);
	reverse(seq.begin(), seq.end());

	return ans;
}

int main()
{
	int n;
	cin>>n;
	vector<int> A(n + 1, 0);
	for(int i = 1; i <= n; i++){
		cin>>A[i];
	}
	cin>>n;
	vector<int> B(n + 1, 0);
	for(int i = 1; i <= n; i++){
		cin>>B[i];
	}

	vector<int> seq;
	cout<<max_len_dp(A, B, seq)<<endl;
	for(int val : seq){
		cout<<B[val]<<" ";
	}
	cout<<endl;

	return 0;
}